/* eslint-disable @typescript-eslint/no-unused-vars */
import { defineStore } from 'pinia';
import type { UserInfoStates, ILogin } from '@/utils/models/login';
import router from '@/router';

export const UseStore = defineStore('UserStore', {
	persist: {
		enabled: true,
		strategies: [
			{
				key: 'UserStore',
				storage: window?.localStorage,
			},
		],
	},

	state: (): UserInfoStates => ({
		userInfo: uni.getStorageSync('userInfo') || {
			userName: '',
			photo: '',
			time: '',
			roles: [],
			authBtnList: [],
			id: '123',
		},
		token: '1111' || uni.getStorageSync('token'),
	}),

	getters: {
		getUserToken: state => state?.token,
		getInfo: (state) => state.userInfo,
	},


	actions: {
		async setUserInfo(data: Partial<ILogin>) {
			this.userInfo = { ...this.userInfo, ...data };
			// const res = await loginAPI(data);
		},

		async setToken(token: string) {
			// 存储用户信息到浏览器缓存
			uni.setStorageSync('token', 'token');
			this.token = token;
		},

		// 获取用户详情
		async getUserInfo({ params = {}, ...options } = {}) {
			console.log('params,options', options, params);
			// const res = await getUserInfo(params, options)

			// if (!res?.success) {
			//   throw new Error('获取用户信息失败')
			// }

			// if (!res?.data) {
			//   throw new Error('获取用户信息失败, 没有获取到数据')
			// }

			// const data = res.data
			// this.userInfo = {
			//   ...data,
			//   userId: data.id,
			//   avatar: data.avatar || defaultAvatar,
			//   username: data.username,
			// }

			// return this.userInfo
		},

		async logout({ silenced = false, redirect = null } = {}) {
			console.log('silenced,redirect', silenced, redirect);
			// this.removeToken()
			// this.userInfo = {}
			// this.resumeInfo = {}

			// if (!silenced) {
			// 	router.replaceAll({
			// 	force: true,
			// 	path: '/login',
			// 	query: {
			// 		redirect,
			// 	},
			// 	})
			// }
		},
	},
});
